import { PlusOutlined } from '@ant-design/icons';
import {
  ModalForm,
  ProForm,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { Button, Form, message, Typography } from 'antd';

import { organEdit } from '@/services/org/OrganService';
import React from 'react';

const { Text, Link } = Typography;

interface Props {
  row?: API.Organ;
  onSuccess?: () => void;
  onFail?: () => void;

  text: string;
  isCard?: boolean;
}

const OrganEdit: React.FC<Props> = (props) => {
  const { row, onSuccess, onFail, text, isCard } = props;

  const [form] = Form.useForm<API.Organ>();
  return (
    <ModalForm<API.Organ>
      title={text}
      initialValues={row}
      trigger={
        isCard ? (
          <Button type="primary">
            <PlusOutlined />
            {text}
          </Button>
        ) : (
          <Button type="link">
            <PlusOutlined />
            {text}
          </Button>
        )
      }
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => {
          console.log('run');
          form.resetFields();
        },
      }}
      submitTimeout={2000}
      onFinish={(values) => {
        // await waitTime(2000);
        console.log(values);

        return new Promise<boolean>((resolve, reject) => {
          organEdit(values)
            .then((res) => {
              if (res.code === 200) {
                resolve(true);
                message.success('提交成功');
                form.resetFields();
                if (onSuccess) {
                  onSuccess();
                }
              } else {
                resolve(false);

                if (onFail) {
                  onFail();
                }
                message.error(res.msg);
              }
            })
            .catch((err) => {
              if (onFail) {
                onFail();
              }
              message.error(err.msg);
              reject(err);
            });
        });
      }}
    >
      <ProForm.Group>
        <ProFormText
          width="md"
          name="id"
          label="名称"
          placeholder="请输入名称"
          initialValue={row?.id}
          hidden={true}
        />

        <ProFormText
          width="md"
          name="name"
          label="名称"
          placeholder="请输入名称"
          required={true}
          rules={[{ required: true }]}
        />

        <ProFormText
          width="md"
          name="shortName"
          label="Short名称"
          placeholder="请输入名称"
          required={true}
          rules={[{ required: true }]}
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormTextArea
          width="xl"
          name="remark"
          label="备注"
          placeholder="请输入名称"
          required={true}
          rules={[{ required: true }]}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default OrganEdit;
